package com.action;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bean.ResponseBean;
import com.pojo.Fandian;
import com.service.FandianService;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @Description:
 * @Auther: tz
 * @Date: 2023/12/1 9:52
 */
@CrossOrigin
@RestController
@RequestMapping("/fandian")
public class FandianAction {

    @Autowired
    private FandianService fandianService;

    /**
     * 根据ID查询返点数据
     * @param id
     * @return
     */
    @RequestMapping("/getFandianById")
    public ResponseBean getFandianById(String id){
        if (StringUtil.isNull(id)){
            return ResponseBean.error("参数错误");
        }
        Fandian fandian = fandianService.getById(id);
        if (fandian == null){
            return ResponseBean.error("为找到数据");
        }
        return new ResponseBean(200,"成功",fandian);
    }

    /**
     * 分页查询返点数据
     * @param page
     * @param limit
     * @param key
     * @return
     */
    @RequestMapping("/getFandianByPage")
    public ResponseBean getFandianByPage(@RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "10")int limit, String key){
        Page p = new Page(page,limit);
        QueryWrapper q = new QueryWrapper();
        if (StringUtil.isNotNull(key)){
            q.like("djName","%"+key+"%");
        }
        q.orderByDesc("createTime");
        IPage iPage = fandianService.page(p,q);
        return new ResponseBean(0,iPage.getTotal(),"成功",iPage.getRecords());
    }

    /**
     * 修改返点
     * @param fandian
     * @return
     */
    @RequestMapping("/updFandian")
    public ResponseBean updFandian(@RequestBody Fandian fandian){
        String msg = fandianService.verify(fandian);
        if (StringUtil.isNotNull(msg)){
            return ResponseBean.error(msg);
        }
        long m = fandianService.count(new QueryWrapper<Fandian>().eq("djName",fandian.getDjName()));
        long n = fandianService.count(new QueryWrapper<Fandian>().eq("djName",fandian.getDjName()).eq("id",fandian.getId()));
        if (n==0 && m>0){
            return ResponseBean.error("档级名称已存在，请重新编辑！");
        }
        if (fandianService.updateById(fandian)){
            return ResponseBean.ok("修改成功");
        }
        return ResponseBean.error("修改失败");
    }

    /**
     * 删除返点
     * @param id
     * @return
     */
    @RequestMapping("/delFandianById")
    public ResponseBean delFandianById(String id){
        if (StringUtil.isNull(id)){
            return ResponseBean.error("参数错误");
        }
        if (fandianService.removeById(id)){
            return ResponseBean.ok("删除成功");
        }
        return ResponseBean.error("删除失败");
    }

    /**
     * 新增返点
     * @param fandian
     * @return
     */
    @RequestMapping("/addFandian")
    public ResponseBean addFandian(@RequestBody Fandian fandian){
        String msg = fandianService.verify(fandian);
        if (StringUtil.isNotNull(msg)){
            return ResponseBean.error(msg);
        }
        long m = fandianService.count(new QueryWrapper<Fandian>().eq("djName",fandian.getDjName()));
        if (m > 0){
            return ResponseBean.error("档级名称已存在，请重新编辑！");
        }
        fandian.setCreateTime(new Date());
        if (fandianService.save(fandian)){
            return ResponseBean.ok("新增成功");
        }
        return ResponseBean.error("新增失败");
    }
}
